package com.yonyou.pmclouds.projectlog.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.project.entity.ProjectVO;
import com.yonyou.pmclouds.project.rmiitf.ProjectSimpleQuery;
import com.yonyou.pmclouds.projectlog.entity.*;
import com.yonyou.pmclouds.projectlog.mapper.ProjectLogDetailMapper;
import com.yonyou.pmclouds.projectlog.mapper.ProjectLogMapper;
import com.yonyou.pmclouds.projectlog.rmiitf.ProjectLogQuery;
import com.yonyou.pmclouds.projectstatus.rmiitf.ProjectStatusQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * 智能日志
 * @author xiejianyu
 * 2019-09-18
 */
@Slf4j
@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = ProjectLogQuery.class)
public class ProjectLogQueryImpl implements ProjectLogQuery {
    @Autowired
    private ProjectLogMapper projectLogMapper;
    @Autowired
    private ProjectLogDetailMapper projectLogDetailMapper;
    @Autowired
    private ProjectSimpleQuery projectSimpleQuery;
    @Autowired
    private ProjectStatusQuery statusQuery;

    public static ObjectMapper mapper = new ObjectMapper();

    @Override
    public ProjectLogVo queryByProjectAndDate(String pkProject, Date date) throws BusinessException {
        try {
            if (StringUtils.isEmpty(pkProject)) {
                throw new BusinessException("缺少项目信息");
            }

            if (date==null||date.after(new Date())) {
                throw new BusinessException("日期不合法");
            }

            ProjectLogVo vo = projectLogMapper.queryProjectLog(RuntimeEnvironment.getTenantId(), pkProject, date);
            if (vo==null) {
                throw new BusinessException("该项目当天没有日志");
            }

            ProjectVO projectVO = projectSimpleQuery.queryProject(pkProject);

            //项目状态处理
            Set<String> states = new HashSet<>();
            states.add(projectVO.getPkProjectStatus());
            states.remove(null);
            // 处理状态
            if (!states.isEmpty()) {
                Map<String, String> statusNameMap = statusQuery.queryNamesByIds(states.toArray(new String[0]));
                if (projectVO.getPkProjectStatus() != null) {
                    vo.setProjectStateName(statusNameMap.get(projectVO.getPkProjectStatus()));
                }
            }

            vo.setConstructionContent(new ArrayList<ConstructionContentVo>());
            vo.setInspectionContent(new ArrayList<InspectionContentVo>());
            vo.setProblemStatistic(new ArrayList<ProblemStatisticVo>());
            vo.setVisaAlteration(new ArrayList<VisaAlterationVo>());
            vo.setOther(new ArrayList<OtherVo>());
            vo.setSafetyRectifyList(new ArrayList<SafetyRectifyContentVO>());

            List<ProjectLogDetailVo> details = projectLogDetailMapper.queryProjectLogDetails(vo.getPkProjectLog());
            for (ProjectLogDetailVo detail: details) {
                switch (detail.getDetailType()){
                    case ProjectLogDetailVo.CONSTRUCTION_CONTENT:
                        ConstructionContentVo constructionContentVo = mapper.readValue(detail.getContentJson(), ConstructionContentVo.class);
                        vo.getConstructionContent().add(constructionContentVo);
                        break;
                    case ProjectLogDetailVo.INSPECTION_CONTENT:
                        InspectionContentVo inspectionContentVo = mapper.readValue(detail.getContentJson(), InspectionContentVo.class);
                        vo.getInspectionContent().add(inspectionContentVo);
                        break;
                    case ProjectLogDetailVo.PROBLEM_STATISTIC:
                        ProblemStatisticVo problemStatisticVo = mapper.readValue(detail.getContentJson(), ProblemStatisticVo.class);
                        vo.getProblemStatistic().add(problemStatisticVo);
                        break;
                    case ProjectLogDetailVo.VISA_ALTERATION:
                        VisaAlterationVo visaAlterationVo = mapper.readValue(detail.getContentJson(), VisaAlterationVo.class);
                        vo.getVisaAlteration().add(visaAlterationVo);
                        break;
                    case ProjectLogDetailVo.OTHER:
                        OtherVo otherVo = mapper.readValue(detail.getContentJson(), OtherVo.class);
                        vo.getOther().add(otherVo);
                        break;
                    case ProjectLogDetailVo.SAFETY_RECTIFY:
                        SafetyRectifyContentVO safetyRectifyContentVO = mapper.readValue(detail.getContentJson(),
                                SafetyRectifyContentVO.class);
                        vo.getSafetyRectifyList().add(safetyRectifyContentVO);
                        break;
                    default:
                        break;
                }
            }

            return vo;
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                throw (BusinessException)e;
            } else {
                log.error("查询智能日志出错", e);
                throw new BusinessException("智能日志服务端查询出错");
            }
        }
    }
}
